package app.ui;

import java.awt.Insets;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import app.action.ExportQuesstionTextDatHelper;
import app.action.MakeDataFileAction;
import app.action.SaveDataFile;
import app.action.SelectMP3FileAction;
import app.action.TextContentAction;

public class MakeDataFileUi extends AbsCommonPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -555605365809585292L;

	private JButton[] jButtons;
	private JTextField[] jTextFields;
	private JLabel jLabel;
	private JComboBox<String> jComboBox;
	private JTextArea jTextArea;
	private JScrollPane jScrollPane;
	private final String[] btnTitles = { "Browse", "Clear", "Add", "Format", "Save", "Close" };
	private final String[] jTextFieldNames = { "textDir", "textQuestionNumber", "textLines" };
	private final String jLabelText = "Amount of question: ";
	private final String[] itemComboBoxs = { "Add Questions", "Add Answers", "Add Tape Script" };
	private ExportQuesstionTextDatHelper expHelper;
	private TextContentAction textContentAction;

	public MakeDataFileUi(MainFrame mFrame) {
		super(mFrame);
		this.expHelper = new ExportQuesstionTextDatHelper(this, new Properties());
		doAddComponents();
		initializeComponents();
	}

	protected void doAddComponents() {
		int num = btnTitles.length;
		jButtons = new JButton[num];
		for (int i = 0; i < num; i++) {
			this.jButtons[i] = new JButton(btnTitles[i]);
			this.add(jButtons[i]);
		}

		num = jTextFieldNames.length;
		jTextFields = new JTextField[num];
		for (int i = 0; i < num; i++) {
			this.jTextFields[i] = new JTextField();
			jTextFields[i].setName(jTextFieldNames[i]);
			this.add(jTextFields[i]);
		}

		this.jLabel = new JLabel(jLabelText);
		this.add(jLabel);

		this.jComboBox = new JComboBox<String>(itemComboBoxs);
		this.add(jComboBox);

		this.jTextArea = new JTextArea();
		this.jScrollPane = new JScrollPane(jTextArea);
		this.add(jScrollPane);
	}

	@Override
	public void initializeComponents() {
		int i = 0;
		MakeDataFileAction makeDataFileAction = new MakeDataFileAction(this);
		for (JButton jButton : jButtons) {
			jButton.setName(btnTitles[i++]);
			jButton.addActionListener(makeDataFileAction);
		} // i != 0;
		jButtons[0].removeActionListener(makeDataFileAction); // Browse
		jButtons[0].addActionListener(new SelectMP3FileAction(this));
		jButtons[4].removeActionListener(makeDataFileAction); // Save
		jButtons[4].addActionListener(new SaveDataFile(this));
		jButtons[2].setEnabled(false);

		jTextFields[0].setEditable(false);
		jTextFields[1].setEditable(false);
		jTextFields[1].setText("-1");
		jTextFields[2].setEditable(false);

		jComboBox.setFont(mFrame.getAppInit().getFont1());

		jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

		this.textContentAction = new TextContentAction(this);
		jTextArea.addMouseListener(textContentAction);
		jTextArea.getDocument().addDocumentListener(textContentAction);
		jTextArea.setFont(mFrame.getAppInit().getFont2());
		jTextArea.setMargin(new Insets(5, 15, 5, 5));

	}

	@Override
	public void resizeComponents() {
		final int wid = this.getWidth() / 4;
		final int hei = 35;
		int width = this.getWidth();
		int height = this.getHeight() - 4 * hei;
		int x = 0;
		int y = 0;

		jScrollPane.setBounds(x, y, width, height); // text area
		jScrollPane.updateUI();
		y += hei;

		x = 0;
		y += this.getHeight() - 4 * hei;
		width -= wid;
		height = hei;
		jTextFields[0].setBounds(x, y, width, height); // textDir
		x += this.getWidth() - wid;
		width = wid;
		jButtons[0].setBounds(x, y, width, height); // Browse

		x = 0;
		y += height;
		width = wid + wid / 2;
		jLabel.setBounds(x, y, width, height); // Amount of question:
		x += width;
		jComboBox.setBounds(x, y, width, height); // Combo box
		width = wid;
		x = this.getWidth() - width;
		jButtons[1].setBounds(x, y, width, height); // Clear

		x = 0;
		y += height;
		width = wid;
		jButtons[2].setBounds(x, y, width, height); // Add
		x += width;
		width = wid / 2;
		jTextFields[1].setBounds(x, y, width, height); // textQuestionNumber
		x += width;
		width = wid;
		jButtons[3].setBounds(x, y, width, height); // Format
		x += width;
		width = wid / 2;
		jTextFields[2].setBounds(x, y, width, height); // textLines
		x += width;
		jButtons[4].setBounds(x, y, width, height); // Save
		width = wid / 2;
		x = this.getWidth() - width;
		jButtons[5].setBounds(x, y, width, height); // Close
	}

	public void updateTextDir(String text) {
		jTextFields[0].setText(text);
	}

	public JButton[] getjButtons() {
		return jButtons;
	}

	public JTextField[] getjTextFields() {
		return jTextFields;
	}

	public JLabel getjLabel() {
		return jLabel;
	}

	public JComboBox<String> getjComboBox() {
		return jComboBox;
	}

	public JTextArea getjTextArea() {
		return jTextArea;
	}

	public JScrollPane getjScrollPane() {
		return jScrollPane;
	}

	public String[] getBtnTitles() {
		return btnTitles;
	}

	public String[] getjTextFieldNames() {
		return jTextFieldNames;
	}

	public String getjLabelText() {
		return jLabelText;
	}

	public String[] getItemComboBoxs() {
		return itemComboBoxs;
	}

	public ExportQuesstionTextDatHelper getExHelper() {
		return expHelper;
	}

	public TextContentAction getTextContentAction() {
		return textContentAction;
	}

	public void updateAfterInsertAQuestion(int numVal) {
		// Show the amount of question
		String amtQueStr = String.valueOf(expHelper.getAmtQue());
		amtQueStr = jLabelText + amtQueStr;
		jLabel.setText(amtQueStr);
		jTextFields[1].setText(String.valueOf(numVal));
	}
}
